package dtec.project.whereareu;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.google.android.gms.maps.model.LatLng;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class UserLocation extends SQLiteOpenHelper {
	private static final String DB_NAME="data.db";
	static final String TB_NAME="UserLocation";
	public static final String COLUMN1="_id";
	public static final String COLUMN2="uid";
	public static final String COLUMN3="longtitude";
	public static final String COLUMN4="latitude";
	public static final String COLUMN5="date";
	public static final String COLUMN6="name";
	private static final int version=1;
	public UserLocation(Context context) {
		super(context, TB_NAME, null, version);
		// TODO Auto-generated constructor stub
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql="CREATE  TABLE "+TB_NAME+" ( "+COLUMN1+" INTEGER PRIMARY KEY  NOT NULL , "+COLUMN2+" INTEGER NOT NULL , "+COLUMN3+" DOUBLE NOT NULL , "+COLUMN4+" DOUBLE NOT NULL , "+COLUMN5+" DATETIME NOT NULL , "+COLUMN6+" VARCHAR  NOT NULL )";
		db.execSQL(sql);
		
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	public void insertData(int id){
		SQLiteDatabase db=this.getWritableDatabase();
	}
	//get user least location
	public LatLng  getUserLocation(int id){
		SQLiteDatabase db=this.getReadableDatabase();
		LatLng point = null;
		String sql="SLECT * FROM "+TB_NAME+" WHERE "+COLUMN2+"="+id+" ORDER BY "+COLUMN1+" DESC";
		Cursor c=db.rawQuery(sql, null);
		c.moveToFirst();
		if(c.getCount()>0){
			point=new LatLng(c.getDouble(c.getColumnIndex(COLUMN4)),c.getDouble(c.getColumnIndex(COLUMN3)));
		}
		return point;
	}
	
	public Cursor  getalldata(){
		SQLiteDatabase db=this.getReadableDatabase();
		String sql="SELECT * FROM "+TB_NAME;
		Cursor c=db.rawQuery(sql, null);
		c.moveToFirst();
		return c;
	}
	//get time
	private String getDateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date date = new Date();
        return dateFormat.format(date);
}
	//add new data
	public long insertData(int num,String date,double latitude,double longtitude,String name){
		ContentValues values = new ContentValues();
		values.put(COLUMN2, num);
		values.put(COLUMN4, latitude);
		values.put(COLUMN3, longtitude);
		values.put(COLUMN5, date);
		values.put(COLUMN6, name);
		SQLiteDatabase db=this.getWritableDatabase();
		long id = db.insert(TB_NAME, null, values);
		db.close();
		return id;
	}
	
	//reset database
	public void resetData(){
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TB_NAME, null,null);
		db.close();
	}
	
	public String getName(int uid){
		String temp=null;
		SQLiteDatabase db=this.getReadableDatabase();
		String sql="SELECT * FROM "+TB_NAME+" WHERE "+COLUMN2+"="+uid;
		Cursor c=db.rawQuery(sql, null);
		c.moveToFirst();
		if(c.getCount()>0){
			temp=c.getString(c.getColumnIndex(COLUMN6));
		}
		return temp;
	}
	
	public LatLng getLocation(int uid){
		LatLng point = null;
		SQLiteDatabase db=this.getReadableDatabase();
		String sql="SELECT * FROM "+TB_NAME+" WHERE "+COLUMN2+"="+uid+" ORDER BY "+COLUMN5+" DESC";
		Cursor c=db.rawQuery(sql, null);
		c.moveToFirst();
		if(c.getCount()>0){
			point=new LatLng(c.getDouble(c.getColumnIndex(COLUMN4)),c.getDouble(c.getColumnIndex(COLUMN3)));
		}
		return point;
	}
	

}
